(function () {
  'use strict';

  angular
    .module('vboxApp')
    .directive('autoFocus', autoFocusDirective);

  /** @ngInject */
  function autoFocusDirective($timeout) {
    return {
      restrict: 'A',
      scope: {
        autoFocus: '='
      },
      link: function(scope, element) {
        scope.$watch('autoFocus', function(newValue) {
          //console.info(newValue)
          if (newValue) {
            $timeout(function(){
              element[0].focus();
            });
          }
        });

        element.on('blur', function() {
          scope.$apply(function() {
            scope.autoFocusWhen = false;
          });
        });
      }
    };
  }

})();
